﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DentScan2.DB;
using AjaxControlToolkit;

namespace DentScan2.WebApp.UC
{

    public partial class ucRegional : Classes.BaseUserControl
    {
        DLDataContext dl = new DLDataContext();

        public Enums.Classificators Classificator
        {
            get
            {
                return (Enums.Classificators)ViewState["Classificator"];
            }
            set
            {
                ViewState["Classificator"] = value;
            }
        }

        public bool CanInsert
        {
            get;
            set;
        }

        public bool AddNull
        {
            get;
            set;
        }

        public bool MandatoryFields
        {
            get;
            set;
        }

        public Guid? CountryId
        {
            get
            {
                return ddlCountry.SelectedValue == "" ? null : Guid.Parse(ddlCountry.SelectedValue) as Guid?;
            }
            set
            {
                DisplayCountries(null);
                ddlCountry.SelectedValue = value.ToString();
                DisplayDistricts(null);
            }
        }

        public Guid? DistrictId
        {
            get
            {
                return ddlDistrict.SelectedValue == "" ? null : Guid.Parse(ddlDistrict.SelectedValue) as Guid?;
            }
            set
            {
                DisplayDistricts(null);
                ddlDistrict.SelectedValue = value.ToString();
                DisplayCities(null);
            }
        }

        public Guid? CityId
        {
            get
            {
                return ddlCity.SelectedValue == "" ? null : Guid.Parse(ddlCity.SelectedValue) as Guid?;
            }
            set
            {
                ddlCity.SelectedValue = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetProperFilter(ftCity);
                SetProperFilter(ftDistrict);
                SetProperFilter(ftCountry);
                if (ddlCountry.Items.Count == 0)
                    DisplayCountries(null);
                if (ddlDistrict.Items.Count == 0)
                    DisplayDistricts(null);
                if (ddlCity.Items.Count == 0)
                    DisplayCities(null);

                if (this.MandatoryFields)
                {
                    ddlCountry.BorderColor = System.Drawing.Color.Red;
                    ddlDistrict.BorderColor = System.Drawing.Color.Red;
                    ddlCity.BorderColor = System.Drawing.Color.Red;
                }
            }
        }

        

        private void DisplayCountries(Guid? id)
        {
            Tools.Tools.ClearDropDownSelection(ddlCountry);
            Guid areaId = (this.Page as Classes.BaseForm).AreaId.Value;
            ddlCountry.DataTextField = "CountryName";
            ddlCountry.DataValueField = "CountryId";
            ddlCountry.DataSource = dl.Countries.Where(item => !item.IsDel && item.AreaId == areaId).OrderBy(item => item.CountryName);
            ddlCountry.DataBind();
            if (AddNull)
                ddlCountry.Items.Insert(0, new ListItem("", ""));
            if (id.HasValue)
                ddlCountry.SelectedValue = id.Value.ToString();
            else
                if (ddlCountry.Items.Count > 0)
                    ddlCountry.Items[0].Selected = true;

            ibAddCountry.Visible = CanInsert;
        }

        private void DisplayDistricts(Guid? id)
        {
            if (ddlCountry.Items.Count > 0)
            {
                Tools.Tools.ClearDropDownSelection(ddlDistrict);
                Guid areaId = (this.Page as Classes.BaseForm).AreaId.Value;
                if (ddlCountry.SelectedValue != "")
                {
                    ddlDistrict.DataTextField = "DistrictName";
                    ddlDistrict.DataValueField = "DistrictId";
                    ddlDistrict.DataSource = dl.Districts.Where(item => !item.IsDel && item.AreaId == areaId && item.CountryId == Guid.Parse(ddlCountry.SelectedValue)).OrderBy(item => item.DistrictName);
                    ddlDistrict.DataBind();
                }
                if (AddNull)
                    ddlDistrict.Items.Insert(0, new ListItem("", ""));

                if (id.HasValue)
                    ddlDistrict.SelectedValue = id.Value.ToString();
                else
                    if (ddlDistrict.Items.Count > 0)
                        ddlDistrict.Items[0].Selected = true;
            }
            else
                ddlDistrict.Items.Clear();

            ibAddDistrict.Visible = CanInsert && ddlCountry.Items.Count > 0;
        }


        private void DisplayCities(Guid? id)
        {
            if (ddlDistrict.Items.Count > 0)
            {
                Tools.Tools.ClearDropDownSelection(ddlCity);
                Guid areaId = (this.Page as Classes.BaseForm).AreaId.Value;
                if (ddlDistrict.SelectedValue != "")
                {
                    ddlCity.DataTextField = "CityName";
                    ddlCity.DataValueField = "CityId";
                    ddlCity.DataSource = dl.Cities.Where(item => !item.IsDel && item.AreaId == areaId && item.DistrictId == Guid.Parse(ddlDistrict.SelectedValue)).OrderBy(item => item.CityName);
                    ddlCity.DataBind();
                }
                if (AddNull)
                    ddlCity.Items.Insert(0, new ListItem("", ""));

                if (id.HasValue)
                    ddlCity.SelectedValue = id.Value.ToString();
                else
                    if (ddlCity.Items.Count > 0)
                        ddlCity.Items[0].Selected = true;
            }
            else
                ddlCity.Items.Clear();

            ibAddCity.Visible = CanInsert && ddlDistrict.Items.Count > 0;
        }


        private void InsertCountry()
        {
            tbCountry.Text = "";
            tbCountry.Focus();
            tbCountry.Visible = true;
            bSaveCountry.Visible = true;
            bCancelCountry.Visible = true;
            ddlCountry.Visible = false;
            ibAddCountry.Visible = false;
        }

        private void InsertDistrict()
        {
            tbDistrict.Text = "";
            tbDistrict.Focus();
            tbDistrict.Visible = true;
            bSaveDistrict.Visible = true;
            bCancelDistrict.Visible = true;
            ddlDistrict.Visible = false;
            ibAddDistrict.Visible = false;
        }

        private void InsertCity()
        {
            tbCity.Text = "";
            tbCity.Focus();
            tbCity.Visible = true;
            bSaveCity.Visible = true;
            bCancelCity.Visible = true;
            ddlCity.Visible = false;
            ibAddCity.Visible = false;
        }

        private void CancelCountry()
        {
            ddlCountry.Visible = true;
            ibAddCountry.Visible = true;
            tbCountry.Visible = false;
            bSaveCountry.Visible = false;
            bCancelCountry.Visible = false;
        }

        private void CancelDistrict()
        {
            ddlDistrict.Visible = true;
            ibAddDistrict.Visible = true;
            tbDistrict.Visible = false;
            bSaveDistrict.Visible = false;
            bCancelDistrict.Visible = false;
        }

        private void CancelCity()
        {
            ddlCity.Visible = true;
            ibAddCity.Visible = true;
            tbCity.Visible = false;
            bSaveCity.Visible = false;
            bCancelCity.Visible = false;
        }

        //private void Save()
        //{
        //    Page.Validate("vgRegional");
        //    if (!Page.IsValid)
        //        return;

        //    Guid id = Guid.NewGuid();
        //    switch (this.Classificator)
        //    {


        //        case Enums.Classificators.Countries:
        //            DB.Country country = new Country()
        //            {
        //                CountryId = id,
        //                CountryName = tb.Text,
        //                IsDel = false,
        //                LastEditDate = DateTime.UtcNow,
        //                AreaId = (this.Page as Classes.BaseForm).AreaId.Value
        //            };
        //            dl.Countries.InsertOnSubmit(country);
        //            dl.SubmitChanges();
        //            DisplayCountries(id);
        //            DisplayDistricts(null);
        //            DisplayCities(null);
        //            break;
        //        case Enums.Classificators.Districts:
        //            DB.District district = new District()
        //            {
        //                DistrictId = id,
        //                DistrictName = tb.Text,
        //                CountryId = Guid.Parse(ddlCountry.SelectedValue),
        //                IsDel = false,
        //                LastEditDate = DateTime.UtcNow,
        //                AreaId = (this.Page as Classes.BaseForm).AreaId.Value
        //            };
        //            dl.Districts.InsertOnSubmit(district);
        //            dl.SubmitChanges();
        //            DisplayDistricts(id);
        //            DisplayCities(null);
        //            break;
        //        case Enums.Classificators.Cities:
        //            DB.City city = new City()
        //            {
        //                CityId = id,
        //                CityName = tb.Text,
        //                DistrictId = Guid.Parse(ddlDistrict.SelectedValue),
        //                IsDel = false,
        //                LastEditDate = DateTime.UtcNow,
        //                AreaId = (this.Page as Classes.BaseForm).AreaId.Value
        //            };
        //            dl.Cities.InsertOnSubmit(city);
        //            dl.SubmitChanges();
        //            DisplayCities(id);
        //            break;
        //    }
        //    mv.SetActiveView(vShow);
        //}

        protected void bSaveCountry_Click(object sender, ImageClickEventArgs e)
        {
            Page.Validate("vgCountry");
            if (!Page.IsValid)
                return;

            Guid id = Guid.NewGuid();
            DB.Country country = new Country()
            {
                CountryId = id,
                CountryName = tbCountry.Text.ToUpper(),
                IsDel = false,
                LastEditDate = DateTime.UtcNow,
                AreaId = (this.Page as Classes.BaseForm).AreaId.Value
            };
            dl.Countries.InsertOnSubmit(country);
            dl.SubmitChanges();

            // скрива контролите за редакция
            CancelCountry();

            DisplayCountries(id);
            DisplayDistricts(null);
            DisplayCities(null);
        }

        protected void bSaveDistrict_Click(object sender, ImageClickEventArgs e)
        {
            Page.Validate("vgDistrict");
            if (!Page.IsValid)
                return;

            Guid id = Guid.NewGuid();
            DB.District district = new District()
            {
                DistrictId = id,
                DistrictName = tbDistrict.Text.ToUpper(),
                CountryId = Guid.Parse(ddlCountry.SelectedValue),
                IsDel = false,
                LastEditDate = DateTime.UtcNow,
                AreaId = (this.Page as Classes.BaseForm).AreaId.Value
            };
            dl.Districts.InsertOnSubmit(district);
            dl.SubmitChanges();

            // скрива контролите за редакция
            CancelDistrict();

            DisplayDistricts(id);
            DisplayCities(null);
        }

        protected void bSaveCity_Click(object sender, ImageClickEventArgs e)
        {
            Page.Validate("vgCity");
            if (!Page.IsValid)
                return;

            Guid id = Guid.NewGuid();
            DB.City city = new City()
            {
                CityId = id,
                CityName = tbCity.Text.ToUpper(),
                DistrictId = Guid.Parse(ddlDistrict.SelectedValue),
                IsDel = false,
                LastEditDate = DateTime.UtcNow,
                AreaId = (this.Page as Classes.BaseForm).AreaId.Value
            };
            dl.Cities.InsertOnSubmit(city);
            dl.SubmitChanges();

            // скрива контролите за редакция
            CancelCity();

            DisplayCities(id);
        }

        protected void bCancelCountry_Click(object sender, ImageClickEventArgs e)
        {
            CancelCountry();
        }

        protected void bCancelDistrict_Click(object sender, ImageClickEventArgs e)
        {
            CancelDistrict();
        }

        protected void bCancelCity_Click(object sender, ImageClickEventArgs e)
        {
            CancelCity();
        }

        protected void ibAddCountry_Click(object sender, ImageClickEventArgs e)
        {
            this.Classificator = Enums.Classificators.Countries;
            InsertCountry();
        }

        protected void ibAddDistrict_Click(object sender, ImageClickEventArgs e)
        {
            this.Classificator = Enums.Classificators.Districts;
            InsertDistrict();
        }

        protected void ibAddCity_Click(object sender, ImageClickEventArgs e)
        {
            this.Classificator = Enums.Classificators.Cities;
            InsertCity();
        }

        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisplayDistricts(null);
            DisplayCities(null);
        }

        protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisplayCities(null);
        }
    }
}